'use strict';

const await = require('await-stream-ready/lib/await');
const pageLimit=10;
const Controller = require('egg').Controller;

class Rescue_stationController extends Controller {
    //模糊查找救助站
    // async searchStation() {
    //     const { ctx,app } = this;
    //     let name=ctx.params.name;
    //     let Op=this.app.Sequelize.Op;
    //     let data=await app.model.RescueStation.findAll({
    //         where:{
    //             station_name:{
    //                 [Op.like]: '%'+name+'%'
    //             }
    //         },
    //     });
    //     ctx.apiSuccess(data);
    // }

    //根据救助站id查找其帖子
    async byStationIdPost() {
        const { ctx,app } = this;
        let id = ctx.params.id;
        let result=await app.model.RescueStation.findAll({
            where: {
                id:id
            },
            include: [{
                model: app.model.Post,
                as:'uii',
                attributes: ['id', 'title']
            }],
        })
        ctx.apiSuccess(result)
    }

    //根据救助站id查找动物列表
    async byStationIdAnimal() {
        const { ctx,app } = this;
        let id=ctx.params.id;
        let data=await app.model.CloudAnimal.findAll({
            where:{
                stationId:id
            },
        });
        ctx.apiSuccess(data);
    }

    //根据动物id查找相关的帖子
    async byAnimalIdPost() {
        const { ctx,app } = this;
        let id=ctx.params.id;
        let data=await app.model.Post.findAll({
            where:{
                animalId:id
            },
        });
        ctx.apiSuccess(data);
    }






    

    //模糊查找救助站
    async searchStation() {
        const { ctx,app } = this;
        let name=ctx.params.name;
        let Op=this.app.Sequelize.Op;
        let data=await app.model.RescueStation.findAll({
            where:{
                station_name:{
                    [Op.like]: '%'+name+'%'
                }
            },
            include: [{
                model: app.model.User,
                as:'ori',
            }]
        });
        ctx.apiSuccess(data);
    }

    //首页查看所有救助站
    async lookStation() {
        const { ctx,app } = this;
        let page = ctx.params.page ? parseInt(ctx.params.page) : 1;
        let limit=pageLimit;
        let offset=(page-1)*limit;
        let data=await app.model.RescueStation.findAll({
            include: {
                model: app.model.User,
                as:'ori',
            }
        }
        );
        ctx.apiSuccess(data);
    }

    //根据id查找具体的救助站信息
    async byIdSearchStation() {
        const { ctx,app } = this;
        let id=ctx.params.id;
        let data=await app.model.RescueStation.findOne({
            where:{
                id:id
            },
            include: {
                model: app.model.User,
                as:'ori',
            }
        });
        ctx.apiSuccess(data);
    }


    //申请修改机构信息
    async updateStation(){
        const { ctx, app } = this;
        let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
        let data =await app.model.RescueStation.findByPk(id);
        if(!data){
            ctx.throw(400,'该机构不存在')
        }
        let { briefIntroduction,introduction,specificLocation,officialPhone,coverImg,catNum,dogNum,funderNum,funderMoney } = ctx.request.body
        data.briefIntroduction=briefIntroduction;
        data.introduction = introduction;
        data.specificLocation =specificLocation;
        data.officialPhone=officialPhone;
        data.coverImg=coverImg;
        data.catNum=catNum;
        data.dogNum=dogNum;
        data.funderNum=funderNum;
        data.funderMoney=funderMoney;
        let res = await data.save();
        ctx.apiSuccess(res);
    }


    //认证为机构
    async idenitfy(){
        const { ctx, app } = this;
        let data =await app.model.User.findByPk(ctx.params.id);
        data.idenitfy=1;
        let { id,briefIntroduction,introduction,specificLocation,stationName,officialPhone,coverImg,catNum,dogNum,funderNum,funderMoney } = ctx.request.body
        stationName=data.username;
        let res = await app.model.RescueStation.create({
            id,
            briefIntroduction,
            introduction,
            specificLocation,
            stationName,
            officialPhone,
            coverImg,
            catNum,
            dogNum,
            funderNum,
            funderMoney
        });
        //ctx.apiSuccess(data);
        ctx.apiSuccess(res);
    }


}

module.exports = Rescue_stationController;
